layui.use(['table', 'admin', 'ax', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    /**
     * 职位表管理
     */
    var info = {
        tableId: "infoTable"
    };

    /**
     * 初始化表格的列
     */
    info.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', sort: true, title: '主键id1'},
            {field: 'name', sort: true, title: '名称'},
            {field: 'totalRun', sort: true, title: '总轮数'},
            {field: 'currentRun', sort: true, title: '当前轮数'},
            {field: 'teamNum', sort: true, title: '队伍数'},
            {field: 'ownGoalNum', sort: true, title: '乌龙球'},
            {field: 'halfGoalNum', sort: true, title: '半场'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    info.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(info.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 导出excel按钮
     */
    info.exportExcel = function () {
        var checkRows = table.checkStatus(info.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 修改职位状态
     */
    info.changeStatus = function (positionId, checked) {
        var ajax = new $ax(Feng.ctxPath + "/position/changeStatus", function (data) {
            Feng.success("修改成功!");
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message);
            table.reload(info.tableId);
        });
        ajax.set("positionId", positionId);
        ajax.set("status", checked);
        ajax.start();
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + info.tableId,
        url: Feng.ctxPath + '/info/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: info.initColumn()
    });

    //监听行单击事件（双击事件为：rowDouble）
    table.on('row(infoTable)', function (obj) {
        var data = obj.data;
        info.openViewDlg(data);
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        info.search();
    });

    // 查看联赛信息
    $('#btnView').click(function () {
        var checkStatus = table.checkStatus('infoTable')
            , data = checkStatus.data;
        info.openViewDlg(data[0]);
    });

    /**
     * 弹出添加对话框
     */
    info.openViewDlg = function (data) {
        window.location.href = Feng.ctxPath + '/info/view?fkLeagueId=' + data.id + '&currentRun=' + data.currentRun + '&teamNum=' + data.teamNum;
    };

    // 导出excel
    $('#btnExp').click(function () {
        info.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + info.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            info.openEditDlg(data);
        } else if (layEvent === 'delete') {
            info.onDeleteItem(data);
        }
    });

    // 修改user状态
    form.on('switch(status)', function (obj) {
        var positionId = obj.elem.value;
        var checked = obj.elem.checked ? true : false;
        info.changeStatus(positionId, checked);
    });
});
